package controller;

import entity.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
//@RequestMapping("/abc")
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("进入HelloController...hello");
        ModelAndView mav = new ModelAndView("index");
        httpServletRequest.setAttribute("message","你好 MVC");
        //mav.addObject("message", "Hello Spring MVC");
        //把请求转发到index.jsp，带上数据
        return mav;
    }

    @RequestMapping("/hi")
    public ModelAndView hi(){
        System.out.println("进入HelloController....hi");
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("message","hi,你好");
        return mv;
    }

    @RequestMapping("/hi2")
    public String hi2(Model model){
        model.addAttribute("message","hi2,你好");
        return "index";
    }

    @RequestMapping("/param")
    public String param(User user,HttpServletRequest request){
        System.out.println("HelloController...param");
        System.out.println(user);
        //获取远程(访问者)的ip
        System.out.println(request.getRemoteAddr());
        //登录成功可以去首页
        if (user.getUserName().equals("admin") && user.getPassword().equals("0000")){
            //跳转到 index
            //return "index";
            //客户端跳转
            return "redirect:hi";
        }
        //客户端跳转
        return "redirect:login.jsp";
    }

}
